package helpers;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Connection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import debugs.TemplateDebug;

import entities.Template;

public class CrawlerHelper {
	public static String rewriteArticle(String content) {
		String result = "";
		
		String url = "http://www.ezrewrite.com/rewrite/rewrite.php";
		
		try {
			Connection conn = Jsoup.connect(url);
			
			Document doc = conn
					.header("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
					.header("accept-Language", "en-us,en;q=0.5")
					.header("host", "www.ezrewrite.com")
					.header("referer", url)
					.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1")
					.data("article", content)
					.data("submit", "Rewrite Article")
					.followRedirects(false)
					.post();
			
			Response response = conn.response();
			
			doc = Jsoup.parse(response.body());
			
			doc.select("ins, script").remove();
			
			result = doc.select("p").html();
			
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return result;
	}
	
	public static String getTag(String content) {
		String result = "";
		
		String url = "http://generatetagger.appspot.com/create-tag";
		
		try {
			Connection conn = Jsoup.connect(url);
			
			Document doc = conn
					.header("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
					.header("accept-Language", "en-us,en;q=0.5")
					.header("host", "generatetagger.appspot.com")
					.header("referer", url)
					.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1")
					.data("content", content)
					.followRedirects(false)
					.post();
			
			Response response = conn.response();
			
			result = response.body();
			
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return result;
	}
	
	public static List<Template> getAllTemplate() {
		List<Template> templates = new ArrayList<Template>();
		
		String url = "http://templatesmanager.appspot.com/service/get-templates";
		
		try {
			InputStream input = new URL(url).openStream();
			Reader json = new InputStreamReader(input, "UTF-8");
			
			Type listType = new TypeToken<List<Template>>(){}.getType();
			templates = new Gson().fromJson(json, listType);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return templates;
	}
	
	public static Template getTemplate(String id) {
		Template template = new Template();
		
		String url = "http://templatesmanager.appspot.com/service/template?id=" + id;
		
		try {
			InputStream input = new URL(url).openStream();
			Reader json = new InputStreamReader(input, "UTF-8");
			
			Type type = new TypeToken<Template>(){}.getType();
			template = new Gson().fromJson(json, type);
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return template;
	}
	
	public static void main(String[] args) {
		
		String url = "http://static.hoantocxoan.com/CJBFK1V4X4A/archives.php";
		
		String content = StringHelper.getHtmlFromUrl(url, "UTF-8");
		
		System.out.println(content);
		
		
		
		
	}
}
